(function($){
    function initProvince() {
        var data = SysArea.province();
        var box = $('#bp_province');
        box.find('option').each(function(i){
            if(i > 0) {
                $(this).remove();
            }
        });
        for(var i=0; i<data.length; i++) {
            box.append('<option value="'+ data[i] +'">'+ data[i] +'</option>');
        }
        $(box.find('option').get(0)).attr('selected', 'selected');
    }
    function initCity(p) {
        var data = SysArea.city(p);
        var box = $('#bp_city');
        box.find('option').each(function(i){
            if(i > 0) {
                $(this).remove();
            }
        });
        for(var i=0; i<data.length; i++) {
            box.append('<option value="'+ data[i] +'">'+ data[i] +'</option>');
        }
        $(box.find('option').get(0)).attr('selected', 'selected');
    }
    function initDistrict(p, c) {
        var data = SysArea.city(p, c);
        var box = $('#bp_district');
        box.find('option').each(function(i){
            if(i > 0) {
                $(this).remove();
            }
        });
        for(var i=0; i<data.length; i++) {
            box.append('<option value="'+ data[i][0] +'">'+ data[i][1] +'</option>');
        }
        $(box.find('option').get(0)).attr('selected', 'selected');
    }
    function initArea() {
        var pObj = $('#bp_province');
        var cObj = $('#bp_city');
        var dObj = $('#bp_district');
        initProvince();
        pObj.on('change', function(){
            var o = $(this);
            initCity(o.val());
        });
        cObj.on('change', function(){
            var o = $(this);
            initDistrict($('#bp_province').val(), o.val());
        });
        var p = pObj.attr('dvalue');
        pObj.val(p);
        initCity(p);
        var c = cObj.attr('dvalue');
        cObj.val(c);
        initDistrict(p, c);
        dObj.val(dObj.attr('dvalue'));
    }
    function initAddPhoto() {
        var but = $('#but_add_photo');
        but.on('click', function(){
            var box = $('#bp_photos_box');
            box.append('<div class="input-group paddingTB5"><input type="file" class="form-control" name="bp_photos[]"><div class="input-group-addon bg_red cursor_pointer" onclick="$.bpPhotoDel(this)"><i class="fa fa-times"></i></div></div>');
        });
    }
    function initSelectMap() {
        $('#but_map_select').on('click', function(){
            layer.open({
                type: 2,
                title: '选择坐标',
                shadeClose: false,
                shade: 0.8,
                area: ['95%', '95%'],
                content: $(this).attr('durl') + '?callback=parent.setMapValue'
            });
        });
    }
    function setMapValue(obj) {
        //console.log(obj);
        $('#bp_coordinate').val(obj.lng + ',' + obj.lat);
        layer.closeAll();
        return true;
    }
    function initSelectTags() {
        $('#but_tags_select').on('click', function(){
            layer.open({
                type: 2,
                title: '选择标签',
                shadeClose: false,
                shade: 0.8,
                area: ['500px', '475px'],
                content: $(this).attr('durl') + '?callback=parent.setTagsValue&tags=' + $('#bp_tags').val()
            });
        });
    }
    function setTagsValue(ids, values) {
        $('#bp_tags').val(ids);
        $('#bp_tags_view').val(values);
        layer.closeAll();
    }
    function formResult(result) {
        layer.closeAll();
        if(result.status != 10000) {
            layer.alert(result.errmsg, {icon: 5});
        } else {
            layer.alert('修改成功！', {icon: 6}, function(index){
                //location.reload(true);
                location.href = $('#form_update').attr('durl');
            });
        }
    }
    function initCover() {
        var boxShow = $('#box_cover_show');
        var boxUpdate = $('#box_cover_update');
        var funcInputFile = function() {
            var inputBox = $('<div class="col-xs-10"><input type="file" class="form-control" id="bp_cover" name="bp_cover"><p class="help-block">图片必须是正方形的，高宽都必须大于200px，大小不得超过2M</p></div>');
            var butBox = $('<div class="col-xs-2"></div>');
            var butCannel = $('<button type="button" class="btn btn-default">取消</button>');
            butCannel.on('click', funcCannel);
            butBox.append(butCannel);
            boxUpdate.append(inputBox);
            boxUpdate.append(butBox);
        };
        var funcCannel = function () {
            boxShow.removeClass('hide');
            boxUpdate.addClass('hide');
            boxUpdate.html('');
        };
        $('#but_cover_update').on('click', function(){
            funcInputFile();
            boxShow.addClass('hide');
            boxUpdate.removeClass('hide');
        });
    }
    function deletePic() {
        var obj = $(this);
        layer.confirm('确定要删除此图片吗？', {btn: ['是', '否']}, function(index){
            layer.load();
            var pId = obj.attr('did');
            var bpId = $('#bp_id').val();
            var url = obj.attr('dhref');
            $.post(url, {bid: bpId, pid: pId}, function(result){
                layer.closeAll();
                if(result.status != 10000) {
                    layer.alert(result.errmsg, {icon: 5});
                } else {
                    $('#bp_photo_' + pId).remove();
                }
            }, 'json');
        }, function(index){
            layer.close(index);
        });
    }
    $.extend({
        'bpPhotoDel': function(obj) {
            $(obj).parent().remove();
        }
    });
    $(function(){
        initArea();
        initAddPhoto();
        initSelectMap();
        initSelectTags();
        initCover();
        window.setMapValue = setMapValue;
        window.setTagsValue = setTagsValue;
        window.formResult = formResult;

        $('#form_update').on('valid.form', function(e, form){
            layer.load();
            form.submit();
        });
        $('.photo_delete_but').on('click', deletePic);


        $('#time').daterangepicker({
            "autoUpdateInput": false,
            "showDropdowns": true,
            "startDate": false,
            "timePicker": true,
            "timePicker24Hour": true,
            "timePickerIncrement": 1,
            "autoApply": true,
            "locale": {
                "format": "HH:mm",
                "separator": " 至 ",
                "applyLabel": "确 定",
                "cancelLabel": "取 消",
                "fromLabel": "From",
                "toLabel": "To",
                "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
                "monthNames": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
                "firstDay": 1
            },
            "opens": "right",
            "drops": "down",
            "buttonClasses": "btn btn-sm",
            "applyClass": "btn-success",
            "cancelClass": "btn-default"
        }, function(start, end, label) {
        });
        $('#time').on('apply.daterangepicker', function(ev, picker) {
            $(this).val(picker.startDate.format('HH:mm') + ' 至 ' + picker.endDate.format('HH:mm'));
        });
        $('#time').on('cancel.daterangepicker', function(ev, picker) {
            $(this).val('');
        });
        
    });
})(jQuery);